package com.unicomcz;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.unicomcz.commision.TotalJf;


public class DetailPageAction extends ActionBase
{
    
    protected int currPage;
    protected int totalCount;
    protected int rowPerpage = 20;
    private int maxPage;
    private List curList;
    private List totalList;
    protected String acctMonth;
    
    
    public int getTotalCount()
    {
        return totalCount;
    }
    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
    }
    public int getMaxPage()
    {
        return maxPage;
    }
    public void setMaxPage(int maxPage)
    {
        this.maxPage = maxPage;
    }
    public int getCurrPage()
    {
        return currPage+1;
    }
    public void setCurrPage(int currPage)
    {
        this.currPage = currPage-1;
    }
    public int getRowPerpage()
    {
        return rowPerpage;
    }
    public void setRowPerpage(int rowPerpage)
    {
        this.rowPerpage = rowPerpage;
    }
    public String getAcctMonth()
    {
        return acctMonth;
    }
    public void setAcctMonth(String acctMonth)
    {
        this.acctMonth = acctMonth;
    }
    public List getCurList()
    {
        return curList;
    }
    public void setCurList(List curList)
    {
        this.curList = curList;
    }
    public List getTotalList()
    {
        return totalList;
    }
    public void setTotalList(List totalList)
    {
        this.totalList = totalList;
    }
    public String getSqlStr() {
        return "";
    }
    public String executeAction(){
        log = Logger.getLogger(this.getClass());  
        totalList = new ArrayList();
        curList = new ArrayList();
        String sqlStr=getSqlStr();
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        
        for (int i=0;(i<rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    public boolean initArray() {
        log = Logger.getLogger(this.getClass());  
        totalList = new ArrayList();
        curList = new ArrayList();
        String sqlStr=getSqlStr();
        if (!query("dls",totalList,sqlStr)) {
            //执行查询失败
            return false;
        }
        maxPage=totalList.size()%rowPerpage == 0? 
                totalList.size()/rowPerpage:totalList.size()/rowPerpage+1;
        totalCount = totalList.size();
        return true;
    }
    public String firstPage(){
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        for (int i=0;(i<rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        currPage = 0;
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    public String lastPage(){
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        for (int i=(maxPage-1)*rowPerpage;
                (i<rowPerpage*rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        currPage = maxPage-1;
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    public String nextPage() {
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        currPage += 1;
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        if (currPage>=maxPage-1) {
            //最后一页
            return lastPage();
        }
        for (int i=currPage*rowPerpage;
                (i<(currPage+1)*rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    public String prioPage() {
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        currPage -= 1;
        if (currPage<=0) {
            //第一页
            return firstPage();
        }
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        for (int i=currPage*rowPerpage;
                (i<(currPage+1)*rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    public String goToPage() {
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        if (currPage<=0) {
            //第一页
            return firstPage();
        }
        if (!initArray()) {
            //执行查询失败
        	tearDown();
            return "fail";
        }
        if (currPage>=maxPage-1) {
            //第一页
            return lastPage();
        }
        for (int i=currPage*rowPerpage;
                (i<(currPage+1)*rowPerpage)&&(i<totalList.size());i++){
            curList.add(totalList.get(i));
        }
        tearDown();
        totalList.clear();
        return SUCCESS;
    }
    
}
